<template>
  <el-dialog
    :top="0"
    :modal =false
    title="器械耗材分类"
    :modal-append-to-body='false'
    :visible.sync="visible">
    版本：
    <el-radio-group v-model="versionFlag"  @change="changeDevicesVersionFlag">
      <el-radio label= 1 >新版</el-radio>
      <el-radio label= 0 >旧版</el-radio>
    </el-radio-group>
    <el-tree
      :data="medListCat"
      :props="medTreeProp"
      node-key="catCode"
      ref="menuListTree"
      show-checkbox>
    </el-tree>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="postMedList">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    name: 'med-cat-dialog',
    props: {
      catIdList: {
        type: Array,
        default: () => {
          return []
        }
      },
      devicesVersionFlag: {
        type: Number,
        default: 0
      }
    },
    data () {
      return {
        visible: false,
        // 医疗器械类别列表（68码）
        medListCat: [],
        medTreeProp: {
          label: 'catName',
          children: 'children'
        },
        versionFlag: this.devicesVersionFlag,
        catList: this.catIdList
      }
    },
    methods: {
      // 向父组件提交耗材分类数据
      postMedList () {
        const arr = this.$refs.menuListTree.getCheckedKeys()
        this.$emit('update:catIdList', arr)
        this.visible = false
      },
      // 初始化
      init () {
        this.$nextTick(() => {
          this.visible = true
          this.getMedList()
        })
      },
      changeDevicesVersionFlag (data) {
        // console.log(data)
        this.$emit('update:devicesVersionFlag', data)
        this.catList = []
        this.getMedList()
      },
      // 获取经营范围
      getMedList () {
        this.$http({
          url: this.$http.adornUrl(`${this.$API.BASE_INFO.MEDICAL_DEVICES_CAT.LIST}`),
          method: 'get',
          params: this.$http.adornParams({
            version: this.versionFlag
          })
        }).then(({data}) => {
          this.medListCat = treeDataTranslate(data, 'catCode')
          this.$refs.menuListTree.setCheckedKeys(this.catList)
        })
      }
    },
    watch: {
      devicesVersionFlag (val) {
        this.versionFlag = val
      },
      catIdList (val) {
        this.catList = val
      }
    }
  }
</script>

<style scoped>

</style>
